// 对象转化成url
let urlEncode = function(param, key, encode) {
  if (param == null) return "";
  var paramStr = "";
  var t = typeof param;
  if (t == "string" || t == "number" || t == "boolean") {
    paramStr +=
      "&" +
      key +
      "=" +
      (encode == null || encode ? encodeURIComponent(param) : param);
  } else {
    for (var i in param) {
      var k =
        key == null
          ? i
          : key + (param instanceof Array ? "[" + i + "]" : "." + i);
      paramStr += urlEncode(param[i], k, encode);
    }
  }
  return paramStr;
};
// 将表单数据添加defaultValue
let addInitialValue = function(formList, value) {
  formList.formList.forEach(item => {
    item.initialValue = value[item.name];
  });
  return formList;
};
// 将数据转为树形结构
function toTreeData(data) {
  let resData = data;
  let tree = [];
  for (let i = 0; i < resData.length; i++) {
    if (resData[i].parentId === "node") {
      let obj = {};
      Object.assign(obj, JSON.parse(JSON.stringify(resData[i])));
      obj.children = [];
      tree.push(obj);
      resData.splice(i, 1);
      i--;
    }
  }
  run(tree);
  function run(chiArr) {
    if (resData.length !== 0) {
      for (let i = 0; i < chiArr.length; i++) {
        for (let j = 0; j < resData.length; j++) {
          if (chiArr[i]._id === resData[j].parentId) {
            let obj = {};
            Object.assign(obj, JSON.parse(JSON.stringify(resData[j])));
            obj.children = [];
            chiArr[i].children.push(obj);
            resData.splice(j, 1);
            j--;
          }
        }
        run(chiArr[i].children);
      }
    }
  }
  tree.forEach(item => {
    if (item.children.length === 0) {
      delete item.children;
    } else {
      item.children.forEach(i => {
        if (i.children.length === 0) {
          delete i.children;
        } else {
          i.children.forEach(n => {
            if (n.children.length === 0) {
              delete n.children;
            }
          });
        }
      });
    }
  });
  return tree;
}
// 将数据转为树形结构用于tree组件
function toTreeDataOne(data) {
  let resData = data;
  let tree = [];
  for (let i = 0; i < resData.length; i++) {
    if (resData[i].parentId === "node") {
      let obj = {};
      obj.title = resData[i].nodeNam;
      obj.key = resData[i]._id;
      obj.children = [];
      tree.push(obj);
      resData.splice(i, 1);
      i--;
    }
  }
  run(tree);
  function run(chiArr) {
    if (resData.length !== 0) {
      for (let i = 0; i < chiArr.length; i++) {
        for (let j = 0; j < resData.length; j++) {
          if (chiArr[i].key === resData[j].parentId) {
            let obj = {};
            obj.title = resData[j].nodeNam;
            obj.key = resData[j]._id;
            obj.children = [];
            chiArr[i].children.push(obj);
            resData.splice(j, 1);
            j--;
          }
        }
        run(chiArr[i].children);
      }
    }
  }
  tree.forEach(item => {
    if (item.children.length === 0) {
      delete item.children;
    } else {
      item.children.forEach(i => {
        if (i.children.length === 0) {
          delete i.children;
        } else {
          i.children.forEach(n => {
            if (n.children.length === 0) {
              delete n.children;
            }
          });
        }
      });
    }
  });
  return tree;
}
export { urlEncode, addInitialValue, toTreeData, toTreeDataOne };
